package com.park.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.model.Arrears;
import com.park.model.Paging;
import com.park.model.PosChargeData;
import com.park.model.SupplementaryRecord;
import com.park.model.UserList;
import com.park.service.ParkUserService;
import com.park.service.SupplementaryRecordService;
import com.park.service.UserListService;
import com.park.service.Utility;

@Controller
@RequestMapping("/supplementaryRecord")
public class SupplementaryRecordController {
	@Autowired
	private SupplementaryRecordService supplementaryRecordService;
	@Autowired
	private UserListService userListService;
	@Autowired
	private ParkUserService parkUserService;
	private static Log logger = LogFactory.getLog(SupplementaryRecordController.class);
	@RequestMapping(value = "/insert", method = RequestMethod.POST, produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String insert(@RequestBody Map<String,Object> args) throws ParseException {
		logger.info("insert参数:"+args);
		int poschargedataid = (int)args.get("poschargedataid");
		int parkid = (int)args.get("parkid");
		String carnumber = (String)args.get("carnumber");
		String operatorid = (String)args.get("operatorid");
		int money = (int)args.get("money");
		String paymenttime = (String)args.get("paymenttime");
		Date payTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(paymenttime);
		int paymentchannel = (int)args.get("paymentchannel");
		int preferentialamount = (int)args.get("preferentialamount");
		int preferentialchannels = (int)args.get("preferentialchannels");
		SupplementaryRecord supplementaryRecord = new SupplementaryRecord(poschargedataid,parkid,carnumber,operatorid,money,null,
				paymentchannel,preferentialamount,preferentialchannels);
		Map<String,Object> resultMap = new HashMap<>();
		int returnCount = supplementaryRecordService.insert(supplementaryRecord);
		if(returnCount == 1) {
			resultMap.put("status", 1001);
			resultMap.put("message", "插入成功");
		}else {
			resultMap.put("status", 1002);
			resultMap.put("message", "插入失败");
		}
		logger.info(resultMap);
		return Utility.gson.toJson(resultMap);
	}

	@RequestMapping(value = "/select", method = RequestMethod.POST, produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String select(@RequestBody Map<String,Object> args) {
		logger.info("select参数:"+args);
		Map<String,Object> resultMap = new HashMap<>();
		int startPage = (int)args.get("startPage");
		int pageSize = (int)args.get("pageSize");
		String plateNumber = StringUtils.isEmpty((String)args.get("plateNumber")) ? null : (String)args.get("plateNumber");
		String operatorId = StringUtils.isEmpty((String)args.get("operatorId")) ? null : (String)args.get("operatorId");
		String parkName = (String)args.get("parkName");
		String portNumber = (String)args.get("portNumber");
		String exitDateStart = (String)args.get("exitDateStart");
		String exitDateEnd = (String)args.get("exitDateEnd");
		int payType =  (int)args.get("payType");
		String username = (String) args.get("username");
		UserList userList = userListService.getByUserName(username);
		List<PosChargeData> posChargeData = null;
		List<Long> ids = new ArrayList<Long>();
		if(userList != null) {
			if(userList.getParentId() != 0) {
				ids = parkUserService.getByUserId(userList.getId());
				if(CollectionUtils.isEmpty(ids)) {
					resultMap.put("chargeMoney",0);
					resultMap.put("paidMoney", 0);
					resultMap.put("status", 1001);
					resultMap.put("message", "用户没有绑定停车场");
					resultMap.put("body", posChargeData);
//					result.put("operatorName", operatorName);
					return Utility.gson.toJson(resultMap);
				}else {
					Arrears arrears = new Arrears(ids,(startPage-1)*pageSize,pageSize,plateNumber,operatorId,parkName,portNumber,exitDateStart,exitDateEnd,payType);
					List<SupplementaryRecord> supplementaryRecords = supplementaryRecordService.select(arrears);
					int allCount = supplementaryRecordService.getCount(arrears);
					Paging page = new Paging(startPage,pageSize,allCount,supplementaryRecords);
					resultMap.put("status", 1001);
					resultMap.put("message", "查询成功");
					resultMap.put("data", page);
					logger.info("select返回值:"+resultMap);
					return Utility.gson.toJson(resultMap);
				}
			}else {
				Arrears arrears = new Arrears(null,(startPage-1)*pageSize,pageSize,plateNumber,operatorId,parkName,portNumber,exitDateStart,exitDateEnd,payType);
				List<SupplementaryRecord> supplementaryRecords = supplementaryRecordService.select(arrears);
				int allCount = supplementaryRecordService.getCount(arrears);
				Paging page = new Paging(startPage,pageSize,allCount,supplementaryRecords);
				resultMap.put("status", 1001);
				resultMap.put("message", "查询成功");
				resultMap.put("data", page);
				logger.info("select返回值:"+resultMap);
				return Utility.gson.toJson(resultMap);
			}
		}else {
			resultMap.put("chargeMoney",0);
			resultMap.put("paidMoney", 0);
			resultMap.put("status", 1001);
			resultMap.put("message", "用户不存在");
			resultMap.put("body", posChargeData);
//			result.put("operatorName", operatorName);
			return Utility.gson.toJson(resultMap);
		}
	}
}
